package com.wangdong.mijiqiqi.bluetooth;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Build;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

/**
 * 作者：wangdong on 2018/11/19 14:50
 * 邮箱：2100520956@qq.com
 * https://www.aliyun.com/jiaocheng/105590.html
 * Android 蓝牙连接汽车OBD设备
 */
//设备连接

public class BluetoothConnect implements Runnable {
    public final static String TAG = BluetoothConnect.class.getSimpleName();
    private static final UUID CONNECT_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    private BluetoothDevice mDevice;
    private BluetoothSocket mSocket;
    private IInterface iInterface;
    private OutputStream out;
    private boolean isConnected = false;

    public BluetoothConnect(BluetoothDevice device) {
        this.mDevice = device;
    }

    public interface IInterface {
        void connected(BluetoothDevice device);

        void receive(String string);

        void disconnect(BluetoothDevice device);

        void connectError();
    }

    @Override
    public void run() {
        try {
            if (iInterface == null) {
//                Log.e("IBluetoothData interface is null");
                return;
            }
            Log.d(TAG,"connecting bluetooth device................");
            int sdk = Build.VERSION.SDK_INT;
            if (sdk >= 10) {
                mSocket = mDevice.createInsecureRfcommSocketToServiceRecord(CONNECT_UUID);
            } else {
                mSocket = mDevice.createRfcommSocketToServiceRecord(CONNECT_UUID);
            }
            mSocket.connect();
            iInterface.connected(mDevice);
            isConnected = true;
            InputStream in = mSocket.getInputStream();
            out = mSocket.getOutputStream();
            String s = "";
            byte[] buffer = new byte[1024 * 3];
            int len;
            while ((len = in.read(buffer)) > 0) {
                s += new String(buffer, 0, len, "GBK");
                int index = -1;
                while ((index = s.indexOf("/r/n")) != -1) {
                    iInterface.receive(s.substring(0, index + 2));
                    s = s.substring(index + 2, s.length());
                    index = -1;
                }
            }
        } catch (IOException e) {
            Log.e(TAG,"errr"+ e.getMessage());
            iInterface.connectError();
        } finally {
            iInterface.disconnect(mDevice);
            isConnected = false;
        }
    }

    public void setInterface(IInterface iInterface) {
        this.iInterface = iInterface;
    }

    public boolean isConnected() {
        return isConnected;
    }

    public void write(byte[] buffer) {
        if (out != null) {
            try {
                out.write(buffer);
                out.flush();
            } catch (IOException e) {
                Log.e(TAG,"write error"+e.getMessage());
            }
        }
    }

    public void close() {
        try {
            if (mSocket != null) {
                mSocket.close();
            }
        } catch (IOException e) {
            Log.e(TAG,"close error"+e.getMessage());
        }
    }
} 